<?php

namespace App\Modules\Home\Models;


use Illuminate\Database\Eloquent\Model;

/**
 * Class Task 地址
 * @package App\Modules\Task\Models
 */
class Address extends Model
{
    protected $table = 'address';

    protected $fillable = ['id', 'user_id', 'consignee', 'tag', 'phone', 'province_id', 'city_id', 'region_id', 'area_name', 'address', 'is_default', 'created_at', 'updated_at', 'status'];

    /**
     * 地址列表
     */
    static function getList($param)
    {
        return self::from('address AS A')
            ->select(['A.id','A.user_id','A.consignee','A.phone','A.province_id','A.city_id','A.region_id','A.tag','A.address','A.is_default','P.name AS province_name','C.name AS city_name','R.name AS region_name'])
            ->leftJoin('area AS P','A.province_id','=','P.id')
            ->leftJoin('area AS C','A.city_id','=','C.id')
            ->leftJoin('area AS R','A.region_id','=','R.id')
            ->where('A.status',1)->where('A.user_id',$param['user_id'])->orderBy('A.is_default','desc')->paginate($param['size']);
    }

    /**
     * 获取地址详情
     * @param $id 任务id
     * @return mixed
     */
    static function getAddress($id)
    {
        return self::from('address AS A')
            ->select(['A.id','A.user_id','A.consignee','A.phone','A.province_id','A.city_id','A.region_id','A.address','A.tag','A.is_default','P.name AS province_name','C.name AS city_name','R.name AS region_name'])
            ->leftJoin('area AS P','A.province_id','=','P.id')
            ->leftJoin('area AS C','A.city_id','=','C.id')
            ->leftJoin('area AS R','A.region_id','=','R.id')
            ->where('A.id',$id)->first();
    }

    /**
     * 添加地址
     * @param $param
     * @return mixed
     */
    static function addAddress($param)
    {
        // TODO check
        return self::create($param);
    }

    /**
     * 更新地址
     * @param $param
     * @return mixed
     */
    static function updateAddress($param)
    {
        // TODO check
        return self::where('id',$param['id'])->update($param);
    }

    /**
     * 更新默认地址
     */
    static function update_default($param){
        // TODO check
        return self::where('user_id',$param['user_id'])->update($param);
    }
}